package com.hk.button;

import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import com.hk.PlayGame;
import com.hk.card.CardTake;
import com.hk.label.NewGameLabel;

public abstract class SuperButton {
	protected NewGameLabel ngLab = NewGameLabel.getProcess();
	protected CardTake ct = CardTake.getPreocess();
	protected PlayGame pg = PlayGame.getProcess();
	protected JButton button;

	protected SuperButton() {
		button = new JButton();
		button.setOpaque(false);
		button.setBorderPainted(false);
		button.setBackground(new Color(0, 0, 0));
		ActionButton();
	}
	protected void BoundsButton(int x, int y, int w, int h) {
		button.setBounds(x, y, w, h);
	}
	protected void SetButtonIcon(String url) {
		button.setIcon(new ImageIcon(url));
	}
	protected abstract void ActionButton();
	public abstract JButton getButton();
	public abstract void ShowButton();
	public abstract void HideButton();

	public void End() {
		int sumcom = ct.getTcc().getSum();
		int sumuser = ct.getTcu().getSum();
		int sumsplit = ct.getTcus().getSum();

		if (pg.isSplit()) {
			if (sumsplit>21) {
				ngLab.setSumsplitLab("bust");
			}
			pg.setSplit(false);
		} else {
			if (sumuser<=21) {
				while (sumcom<17 ) {
					ct.ComTake();
					sumcom = ct.getTcc().getSum();
				}
			}
			LabelChange(sumcom, sumuser, sumsplit);
			MoneyChange(sumcom, sumuser, sumsplit);
			pg.setEnd(true);
		}
		ngLab.updateLabel();
	}
	private void MoneyChange(int sumcom, int sumuser, int sumsplit) {
		if (sumuser>21 || (21>=sumcom && sumcom>sumuser)) {
			pg.setMoney(pg.getMoney()-pg.getBatting());
			pg.setWin(false);
		} else {
			pg.setMoney(pg.getMoney()+pg.getBatting());
			pg.setWin(true);
		}
		if (sumsplit>0) {
			if (sumsplit>21 || (21>=sumcom && sumcom>sumsplit)) {
				pg.setMoney(pg.getMoney()-pg.getBatting());
			} else {
				pg.setMoney(pg.getMoney()+pg.getBatting());
			}
		}
	}

	private void LabelChange(int sumcom, int sumuser, int sumsplit) {
		if (sumcom<=21) {
			ngLab.setSumcomLab("" + sumcom);
		} else {
			ngLab.setSumcomLab("bust");
		}
		if (sumuser<=21) {
			ngLab.setSumuserLab("" + sumuser);
		} else {
			ngLab.setSumuserLab("bust");
		}
		if (sumsplit<=21) {
			ngLab.setSumsplitLab("" + sumsplit);
		} else {
			ngLab.setSumsplitLab("bust");
		}
	}
}
